auto()
// if(!requestScreenCapture()){
//   toast('请求截图失败')
//   exit()
// }
var screenWidth = device.width // 分辨率
var screenHeight = device.height
console.log('screenHeight', screenHeight)
console.log('screenWidth', screenWidth)
const allScreen = [2160, 2340, 2400, 2712]
if (!allScreen.includes(screenWidth)) {
  screenHeight = 2400
  screenWidth = 1080
}
// sleep(2000)
var taskMain = 3
const points = require(`./${screenHeight}x${screenWidth}.js`)
console.log('points')

setScreenMetrics(screenWidth, screenHeight)
var doShengchanxian = true
function clickFun(point, sleepTime) {
  if (!sleepTime) {
    sleepTime = 0.9
  }
  const wScale = 1 // device.width / screenWidth
  const hScale = 1 // device.height / screenHeight
  const pointX = Math.floor(point[0] * wScale + (5 - Math.random() * 10))
  const pointY = Math.floor(point[1] * hScale + (5 - Math.random() * 10))
  click(pointX, pointY)
  const st = sleepTime * 1000 + Math.random() * 500
  sleep(st)
}
function move(dir, time) {
  const center = [380, 850] // 移动中心
  const dirObj = {
    l: [80, 850],
    r: [800, 850],
    t: [380, 500],
    d: [380, 980],
    lt: [225, 696],
    ld: [200, 980],
    rt: [567, 714],
    rd: [500, 980]
  }
  const centerX = center[0]
  const centerY = center[1]
  const toX = dirObj[dir][0]
  const toY = dirObj[dir][1]
  const dur = time * 1000 - 150
  press(centerX, centerY, 150)
  swipe(centerX, centerY, toX, toY, dur)
}
function randomMove(time) {
  var allTime = time
  const allDirect = ['l', 'r', 't', 'd', 'lt', 'ld', 'rt', 'rd']
  var dir = 0
  while (allTime > 0) {
    var randomDir = allDirect[dir % 8]
    var randomTime = Math.floor(Math.random() * 2 + 1)
    move(randomDir, randomTime)
    allTime -= randomTime
    dir++
  }
}
function autoAccept(time, taskStep, fun) {
  if (!taskStep) {
    taskStep = 0
  } else if (taskStep < -3) {
    taskStep = -taskMain + 1
  }
  if (time) {
    clickFun(points.skip)
    clickFun(points.richeng)
    clickFun(points.taskBtn[taskMain - 1 + taskStep], time)
    clickFun(points.firSel)
    clickFun(points.skip, 1)
    fun(2)
  }
}
var dididaTime = 0

function didida(time) {
  console.log('滴滴答开始')
  clickFun(points.taskFirst, time)
  clickFun(points.firSel)
  clickFun(points.skip)
  doAnswer()
  clickFun(points.blank)
  clickFun(points.commit)
  clickFun(points.skip)
  clickFun(points.firSel)
  clickFun(points.skip)
  clickFun(points.blank)
  doAnswer()
  console.log('滴滴答结束')
  if (dididaTime === 0) {
    dididaTime++
    autoAccept(3, 0, didida)
  } else {
    autoAccept(45, 0, wenda)
  }
  // 接着下一个任务
}
function doAnswer() {
  clickFun(points.commit)
  clickFun(points.skip)
  clickFun(points.firSel)
  clickFun(points.skip)
  clickFun(points.blank)
  clickFun(points.secSel)
  clickFun(points.skip)
  clickFun(points.thirdSel)
  clickFun(points.skip)
  clickFun(points.blank)
}
function doCaiji(time) {
  clickFun(points.taskFirst, time)
  clickFun(points.commit, 5)
  clickFun(points.commit, 5)
  clickFun(points.commit, 5)
  clickFun(points.commit, 5)
  clickFun(points.commit, 5)
  clickFun(points.commit, 5)
  clickFun(points.taskFirst, time)
  clickFun(points.skip)
  clickFun(points.submit)
  clickFun(points.commit)
  clickFun(points.skip)
  clickFun(points.submit)
  clickFun(points.skip)
  clickFun(points.commit)
  clickFun(points.skip)
}
function gaoliang(time) {
  clickFun(points.skip, time)
  clickFun(points.skip)
  console.log('采集高粱开始')
  doCaiji(25)
  console.log('采集高粱结束')
  autoAccept(45, 0, xiaomai)
}
function xiaomai(time) {
  clickFun(points.skip, time)
  clickFun(points.skip)
  console.log('采集小麦开始')
  doCaiji(12)
  console.log('采集小麦结束')
  autoAccept(12, 0, gongfa)
}
function doXunbao(time) {
  clickFun(points.taskFirst, time)
  clickFun(points.close)
  clickFun(points.taskFirst, 10)
  clickFun(points.close)
  clickFun(points.taskFirst, 10)
  clickFun(points.close)
  clickFun(points.taskFirst, 10)
  clickFun(points.close)
  clickFun(points.taskFirst, 10)
  clickFun(points.close)
  clickFun(points.taskFirst, time)
  clickFun(points.skip)
  clickFun(points.blank)
}
function gongfa(time) {
  clickFun(points.skip, time)
  clickFun(points.skip)
  console.log('工法寻宝开始')
  doXunbao(25)
  console.log('工法寻宝结束')
  autoAccept(8, 0, jieqi)
}
function jieqi(time) {
  clickFun(points.skip, time)
  clickFun(points.skip)
  console.log('节气寻宝开始')
  doXunbao(45)
  console.log('节气寻宝结束')
  autoAccept(26, 1, daocao)
}
function daocao(time) {
  console.log('割稻草开始')
  clickFun(points.skip, time)
  clickFun(points.skip)
  clickFun(points.taskFirst, 4)
  clickFun(points.skip)
  clickFun(points.secSel)
  clickFun(points.challenge, 5)
  clickFun(points.blank)
  clickFun(points.blank)
  clickFun(points.blank)
  clickFun(points.blank)
  clickFun(points.blank, 3)
  move('t', 2)
  move('rt', 2)
  randomMove(140)
  clickFun(points.taskFirst, 20)
  clickFun(points.taskFirst, 5)
  clickFun(points.taskFirst, 5)
  clickFun(points.taskFirst, 5)
  clickFun(points.taskFirst, 5)
  clickFun(points.skip)
  clickFun(points.blank)
  clickFun(points.submit)
  clickFun(points.blank)
  clickFun(points.taskFirst)
  clickFun(points.skip)
  clickFun(points.blank)
  console.log('割稻草结束')
  autoAccept(65, 2, zoujie)
}

function zoujie(time) {
  console.log('走街串巷开始')
  clickFun(points.taskFirst, time)
  clickFun(points.firSel)
  clickFun(points.skip)
  console.log('第1次')
  dozoujie()
  console.log('第2次')
  dozoujie()
  console.log('第3次')
  dozoujie()
  console.log('第4次')
  dozoujie()
  console.log('第5次')
  dozoujie()
  console.log('走街串巷结束')
  autoAccept(10, 1, jiejingchangqu)
}
function dozoujie() {
  clickFun(points.commit)
  clickFun(points.firSel)
  clickFun(points.skip)
  clickFun(points.taskFirst, 60)
  clickFun(points.skip)
  clickFun(points.submit)
  clickFun(points.taskFirst, 60)
  clickFun(points.skip)
  clickFun(points.blank)
}
function shengchanxian(time) {
  console.log('生产线开始')
  clickFun(points.taskFirst, time)
  clickFun(points.firSel)
  clickFun(points.skip)
  clickFun(points.taskFirst, 40)
  clickFun(points.skip)
  clickFun(points.blank)
  clickFun(points.taskFirst, 10)
  clickFun(points.skip)
  clickFun(points.submit)
  clickFun(points.blank)
  clickFun(points.taskFirst, 5)
  clickFun(points.skip)
  clickFun(points.secSel, 1)
  console.log('小麦研磨')
  gameDoEnd() // 小麦
  clickFun(points.taskFirst, 14)
  clickFun(points.skip)
  clickFun(points.submit)
  clickFun(points.taskFirst, 3)
  clickFun(points.skip)
  clickFun(points.secSel, 1)
  console.log('制曲')
  gameDoEnd() // 制曲
  clickFun(points.taskFirst, 8)
  clickFun(points.skip)
  clickFun(points.submit)
  clickFun(points.taskFirst, 3)
  clickFun(points.skip)
  clickFun(points.secSel, 1)
  console.log('磨制曲粉')
  gameDoEnd()
  clickFun(points.taskFirst, 4)
  clickFun(points.skip)
  clickFun(points.submit)
  clickFun(points.blank)
  clickFun(points.taskFirst, 45)
  clickFun(points.skip)
  clickFun(points.blank)
  clickFun(points.taskFirst, 5)
  clickFun(points.skip)
  clickFun(points.submit)
  clickFun(points.blank)
  clickFun(points.taskFirst, 13)
  clickFun(points.skip)
  clickFun(points.submit)
  clickFun(points.blank)
  clickFun(points.taskFirst, 10)
  clickFun(points.skip)
  clickFun(points.submit)
  clickFun(points.blank)
  clickFun(points.taskFirst, 10)
  clickFun(points.skip)
  clickFun(points.secSel, 1)
  console.log('消消乐')
  gameDoEnd()
  clickFun(points.taskFirst, 20)
  clickFun(points.skip)
  clickFun(points.submit)
  clickFun(points.blank)
  clickFun(points.autoUseGood) // 自动使用基酒
  console.log('生产线结束')
}
function gameDoEnd() {
  clickFun(points.gameStart, 2)
  clickFun(points.blank)
  clickFun(points.blank)
  clickFun(points.blank)
  clickFun(points.blank)
  clickFun(points.blank, 4)
  clickFun(points.gameBack)
  clickFun(points.gameConfirm, 5)
  clickFun(points.gameEnd, 2)
}
function jiejingchangqu(time) {
  console.log('洁净厂区开始')
  clickFun(points.taskFirst, time)
  for (let index = 0; index < 7; index++) {
    console.log('洁净厂区第' + index)
    randomMove(4)
    clickFun(points.taskFirst, 40)
    clickFun(points.commit, 5)
  }
  clickFun(points.skip)
  clickFun(points.blank)
  console.log('洁净厂区结束')
  goDance()
}
function goDance() {
  clickFun(points.richeng)
  clickFun(points.task1Go)
  console.log('前往舞蹈区')
  engines.stopAll()
  exit()
}
function wenda(time) {
  console.log('知识问答开始')
  clickFun(points.taskFirst, time)
  clickFun(points.firSel)
  clickFun(points.skip)
  doAnswer()
  clickFun(points.blank)
  clickFun(points.commit)
  clickFun(points.skip)
  clickFun(points.firSel)
  clickFun(points.skip)
  clickFun(points.blank)
  doAnswer()
  console.log('知识问答结束')
  autoAccept(45, 0, gaoliang)
}

function dati() {
  console.log('巽风达人开始')
  clickFun(points.blank)
  clickFun(points.datiBtn) // 答人按钮位置
  for (let index = 0; index < 11; index++) {
    clickFun(points.datiAnswer, 4) // 选项1
  }
  clickFun(points.datiClose) // 关闭弹窗
  clickFun(points.richeng)
  clickFun(points.blank)
  clickFun(points.receive)
  clickFun([314, 44])
  console.log('巽风达人结束')
  autoAccept(2, doShengchanxian ? -1 : 0, jiejingchangqu)
}
function autoAcceptUndo(time) {
  clickFun(points.goAllTask)
  clickFun(points.canAccept)
  const taskUndoName = imageClipOcr(points.canDoTaskText, true)
  if (taskUndoName) {
    clickFun(points.toNowCanDo, time)
    clickFun(points.firSel)
    clickFun(points.skip, 1)
    doNowFun()
  } else {
    clickFun(points.allTaskClose)
    clickFun(points.richeng)
    const isHasUn = imageClipOcr(points.richengTask3Text, true)
    if (isHasUn === '前往') {
      clickFun(points.task3Go, time)
      doNowFun()
    } else {
      clickFun(points.task1Go)
      exit()
    }
  }
}
var autoTask = function (text) {
  console.log('text', text)
  if (text.includes('每日')) {
    didida(3)
  } else if (text.includes('野生') || text.includes('调研')) {
    if (text.includes('小')) {
      xiaomai(2)
    } else {
      gaoliang(2)
    }
  } else if (text.includes('工法') || text.includes('寻宝')) {
    if (text.includes('气')) {
      jieqi(2)
    } else {
      gongfa(2)
    }
  } else if (text.includes('草') || text.includes('稻草')) {
    daocao(2)
  } else if (text.includes('走') || text.includes('走街')) {
    zoujie(2)
  } else if (text.includes('酿酒')) {
    shengchanxian(2)
  } else if (text.includes('洁净')) {
    jiejingchangqu(2)
  } else if (text.includes('知识')) {
    wenda(2)
  } else if (text.includes('资格')) {
    autoAcceptUndo(30)
  } else if (text === '') {
    clickFun(points.skip)
    clickFun(points.blank)
    autoAcceptUndo(2)
  }
}
var imageClipOcr = function (point, isStop) {
  // sleep(2000)
  var cp = images.captureScreen()
  var clip = images.clip(cp, point[0], point[1], point[2], point[3])
  var text = ocr(clip)

  if (isStop) {
    console.log('text', text)
  } else {
    toast('当前任务: ' + text)
    autoTask(text)
  }

  return text
}
function doNowFun() {
  // imageClipOcr(mainTaskText)
}
function ocr(img) {
  // 识别图片中的文字，返回完整识别信息（兼容百度OCR格式）。
  //可选语言：拉丁 "la" , 中文 "zh" ,梵文 "sa" ,日语 "ja" , 韩语 "ko"
  let result = gmlkit.ocr(img, 'zh')
  //排序 sort()会改变原对象，sorted() 不会改变原对象，而是返回新对象
  result.sort()
  //或者
  //let newResult = result.sorted()
  console.log('文本识别信息: ' + result.text)
  // 回收图片
  img.recycle()
  return result.text
}

function all(tm) {
  if (tm === 'not') {
    doShengchanxian = false
  } else if (tm) {
    taskMain = tm
  }
  autoAccept(3, 0, didida)
  // didida(3)
  // didida(3)
  // gaoliang(25)
  // xiaomai(45)
  // gongfa(12)
  // jieqi(9)
  // daocao(24)
  // zoujie(60)
  // shengchanxian(7)
  // wenda(24)
  // dati()
  // jiejingchangqu(3)
}
// all()
const actions = {
  didida: didida,
  all: all,
  gaoliang: gaoliang,
  xiaomai: xiaomai,
  gongfa: gongfa,
  jieqi: jieqi,
  daocao: daocao,
  zoujie: zoujie,
  shengchanxian: shengchanxian,
  jiejingchangqu: jiejingchangqu,
  wenda: wenda,
  dati: dati,
  ocr: doNowFun
}
// module.exports = actions;

//监听事件
events.on('action', function (actionName, actionParams) {
  console.log('actionName, actionParams', actionName, actionParams)
  clickFun(points.blank)
  actions[actionName](actionParams)
})
//保持脚本运行
setInterval(() => {}, 1000)
// autoAccept(3, -5, shengchanxian)
